package com.acane.instagram.controller;

import com.acane.instagram.mapper.ChatMessageMapper;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.dto.ChatDTO;
import com.acane.instagram.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @Autowired
    private ChatMessageMapper chatMapper;

    @GetMapping("/messages")
    public List<ChatDTO> getMessages(@RequestParam String receiver, HttpSession session) {
        // 这里可以添加权限验证，确保只有当前用户才能获取自己的聊天记录
        String userName = (String) session.getAttribute("user");
        if (userName.equals(receiver)) {
            return chatService.getMessagesByUser(receiver);
        } else {
            // 返回空列表或抛出异常
            return Collections.emptyList();
        }
    }

    @GetMapping("/history")
    public Result getHistory(@RequestParam String sender, @RequestParam String receiver) {
        return Result.success(chatService.getHistory(sender, receiver));
    }

    @GetMapping("/list")
    public Result getChatList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String sender,      // 接收null而不是空字符串
            @RequestParam(required = false) String receiver,
            @RequestParam(required = false) String content,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,  // 改为LocalDate
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(required = false) String mediaType
    ) {
        // 转换日期为带时间的格式
        LocalDateTime startTime = startDate != null ? startDate.atStartOfDay() : null;
        LocalDateTime endTime = endDate != null ? endDate.atTime(23,59,59) : null;
        // 计算分页偏移量
        int offset = (page - 1) * size;

        // 获取数据（新增mediaType参数）
        List<ChatDTO> data = chatMapper.selectChatList(
                sender,
                receiver,
                content,
                startTime,
                endTime,
                mediaType,  // 新增参数
                offset,
                size
        );

        // 获取总数（新增mediaType参数）
        int total = chatMapper.countChatList(
                sender,
                receiver,
                content,
                startTime,
                endTime,
                mediaType  // 新增参数
        );

        return Result.success(data, total);
    }


}
